* Using dataset "income activities" may be encrypted away for confidentiality. Yields incomes.dta/csv. If the de-identified file doesn't contain the
* relevant information, get in touch with Elliott Collins for the raw data. (elliottmcollins@berkeley.edu)

clear
clear matrix
set more off

* Elliott
**cd Z:\home\elliott\Dropbox\1Spring2016\TUP\data\

use "Baseline\income activities.dta", clear

** Clean-up on specific observations
	replace rname = "PERINA GUWA" if rname=="PPRINA GUWA"
	drop if rname=="PPRINA GUWA" & ln!=1

	replace s32 = 27 if s32oth =="ARMY MAN" | s32oth =="SOLDIER" |  s32oth =="POLICEMAN" |  s32oth =="WORKS IN THE ARMY"
	replace s32 = 2 if s32oth =="2"
	replace s32 = 30 if s32oth =="30"
	replace s32 = 12 if s32oth =="TAILORING"
	replace s32 = 4 if s32oth == "FETCHING WATERFOR CONSTRUCTION"
	replace s32 = 20 if s32oth == "WATCHMAN"
	replace s32 = 11 if s32oth =="MAKING LOCAL BREW"

drop s32oth
rename in _i
gen self = strpos(rname, s31)
order  idno rname s31 self ln _i

keep if ln==1 | ln==2 | self==1

drop s31

*** Ridiculous Hack to get Line Letters in order.
foreach iter in 1 2 3 4{
	sort idno ln _i
	replace _i="B" if _i[_n-1]=="A" & _i=="A" & idno==idno[_n-1] & rname==rname[_n-1]
	replace _i="C" if _i[_n-1]=="B" & _i=="B" & idno==idno[_n-1] & rname==rname[_n-1]
	replace _i="D" if _i[_n-1]=="C" & _i=="C" & idno==idno[_n-1] & rname==rname[_n-1]
}
gen ln_i = "_"+string(ln)+_i
drop if ln>2 & self==0
drop ln _i self
gen wdays = s33*s34
keep if wdays > 50

** Pull out some income information before dropping it
** First, Topcode

	foreach V of var s39 s310 {
		sum `V', d
		replace `V'=r(p95) if `V'>r(p95)
		replace `V'=. if `V'==0
	}

** Okay, so what remains to be done here is to get from TUP_merged a list of the average number of people who have eaten at their house in the past 3
** days and normalize the USD numbers by that. It's _plausible_, though still maybe not exactly likely, that with Topcoding and normalizing, the numbers
** will start to look plausible enough to add a caveat to the report rather than a complete dismisal. 

gen yearcheckUSD = s33*s39/3
gen year_reportUSD = s310/3
by idno: replace yearcheckUSD = sum(yearcheckUSD)
by idno: replace year_reportUSD = sum(year_reportUSD)

drop  s33 - s310
reshape wide s32* wdays yearcheckUSD year_reportUSD, i(idno) j(ln_i) string
order idno rname s*, alpha
order  idno rname s32*

* Recode occupation categories
gen housewife = 0

local employ 3 4 5 10 17 26 27
gen r_employ = 0
gen h_employ = 0
label var r_employ "Resp Employed"
label var h_employ "Husb Employed"

local skilled 20 21 22 23 24 25 28 29
gen r_skill = 0
gen h_skill = 0
label var r_skill "Resp Skilled"
label var h_skill "Husb Skilled"

local agric 2 6 7 8 9
gen r_agri = 0
gen h_agri = 0
label var r_agri "Resp Agri"
label var h_agri "Husb Agri"

local busin 11 12 13 14 15 16 18 19 
gen r_busin = 0
gen h_busin = 0
label var r_busin "Resp Business"
label var h_busin "Husb Business"

local livestock 6 7 
gen r_livestock = 0
gen h_livestock = 0
label var r_livestock "Resp Livestock"
label var h_livestock "Husb Livestock"

** Respondent occupation

foreach S of varlist s32_1* {
	replace housewife = 1 if `S'==1
	foreach F of local employ {
	replace r_employ=1 if `S'==`F'
	}
	foreach F of local skilled {
	replace r_skill=1 if `S'==`F'
	}
	foreach F of local agric {
	replace r_agri=1 if `S'==`F'
	}
	foreach F of local busin {
	replace r_busin=1 if `S'==`F'
	}
	foreach F of local livestock {
	replace r_livestock=1 if `S'==`F'
	}
}

** Other HH member occupation

foreach S of varlist s32_2* {

	foreach F of local employ {
	replace h_employ=1 if `S'==`F'
	}
	foreach F of local skilled {
	replace h_skill=1 if `S'==`F'
	}
	foreach F of local agric {
	replace h_agri=1 if `S'==`F'
	}
	foreach F of local busin {
	replace h_busin=1 if `S'==`F'
	}
	foreach F of local livestock {
	replace h_livestock=1 if `S'==`F'
	}
}

* keep idno rname r_* h* wdays*

sum r_* h_*
* sutex
sort idno
* De-identify and save. 
drop rname
save Baseline\incomes, replace

* use Baseline\TUP_baseline_full, clear
* merge 1:1 idno using Baseline\incomes
* rename _m merge_income
* save Baseline\TUP_baseline_full, replace



